package com.vitabuybuy.tv.ui.mine.talent.usercenter

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityTalentWalletBinding
import com.vitabuybuy.tv.dto.BankCardListDto
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.UniversalityResultActivity
import com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel.TalentWalletViewModel
import kotlinx.android.synthetic.main.activity_talent_wallet.*
import java.math.BigDecimal

class TalentWalletActivity : BaseActivity<TalentWalletViewModel, ActivityTalentWalletBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_talent_wallet

    private var bankCardId = -1

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.wallet))
        mTvRight.apply {
            text = getString(R.string.bill)
            visibility = View.VISIBLE
            setOnClickListener {
                TalentWithdrawRecordActivity.openActivity(mContext,getString(R.string.expense))
            }
        }
    }

    override fun observeData() {
        viewModel.apply {
            getMyWallet()
            myWallet.observe(this@TalentWalletActivity, Observer {
                tvWithdrawableBalance.text =
                    String.format(getString(R.string.withdrawable_balance), it.balanceAmount)
                tvHandlingFee.text = String.format(getString(R.string.handling_fee),it.withdrawalRate)
            })
        }
    }

    override fun eventHandling() {
        setOnclickNoRepeat(tvWithdrawVerify, rlChooseAccount,tvAllWithDraw,
                btnWithDraw){v->
            when (v) {
                tvAllWithDraw->{
                    etWithdrawAmount.setText(viewModel.myWallet.value?.balanceAmount.toString())
                }
                btnWithDraw->{
                    if (bankCardId == -1) {
                        XToast.normal(getString(R.string.pls_choose_bank_card))
                        return@setOnclickNoRepeat
                    }
                    var withAmount = etWithdrawAmount.text.toString()
                    if (withAmount.isEmpty()) {
                        XToast.normal(getString(R.string.pls_enter_with_amount))
                        return@setOnclickNoRepeat
                    }
                    if (withAmount.startsWith(".")) {
                        withAmount = "0$withAmount"
                    }
                    if (withAmount.endsWith(".")) {
                        withAmount += "00"
                    }
                    if (withAmount.toBigDecimal().compareTo(viewModel.myWallet.value?.balanceAmount?.toBigDecimal()) == 1) {
                        XToast.normal(getString(R.string.more_than_amount))
                        return@setOnclickNoRepeat
                    }
                    if(withAmount.toBigDecimal().compareTo(BigDecimal.ZERO) == 0){
                        XToast.normal(getString(R.string.pls_enter_with_amount_2))
                        return@setOnclickNoRepeat
                    }
                    viewModel.withDraw(bankCardId, withAmount)
                }
                tvWithdrawVerify -> TalentIdentityAuthenticationActivity.openActivity(mContext)
                rlChooseAccount -> TalentWithdrawAccountActivity.openActivity(mContext,0x11)
            }
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                etWithdrawAmount.setText("")
                viewModel.getMyWallet()
                UniversalityResultActivity.openActivity(mContext, UniversalityResultActivity.FROM_WITH_DRAW_SUCCESS)
            }
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        if (requestCode == 0x11) {
            val bankCardDto = data?.extras?.getSerializable("bankDto") as BankCardListDto
            bankCardId = bankCardDto.id
            tvBank.text = bankCardDto.bankName + "(" + String.format(
                getString(R.string.weihao),
                bankCardDto.number
            ) + ")"
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, TalentWalletActivity::class.java)
        }
    }
}
